package com.appspot.didit.client.tag;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "false")
public class Tag implements Serializable {

	private static final long serialVersionUID = 4465539893422536165L;

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	protected Long id;
	@Persistent
	protected String tagname;
	@Persistent
	protected ArrayList<Long> item_id;
	@Persistent
	Long creator_id;
	@Persistent
	protected String creatorname;

	public String getCreatorname() {
		return creatorname;
	}

	public void setCreatorname(String creatorname) {
		this.creatorname = creatorname;
	}

	@Persistent
	protected ArrayList<Long> user_ids;
	@Persistent
	protected String tagSignature;

	@Persistent
	protected Date projectBegin;

	@Persistent
	protected Date projectEnd;

	@Persistent
	protected Double projectCost;
	// @Persistent(serialized = "true")
	// protected SignatureMap tagSignatures;

	@Persistent
	protected ArrayList<String> usernames;

	@Persistent
	protected ArrayList<Long> relatedTags;

	@Persistent
	protected ArrayList<String> relatedUsernames;

	public ArrayList<String> getRelatedUsernames() {
		if (relatedUsernames == null) {
			relatedUsernames = new ArrayList<String>();
		}
		return relatedUsernames;
	}

	public void setRelatedUsernames(ArrayList<String> relatedUsernames) {
		this.relatedUsernames = relatedUsernames;
	}

	public ArrayList<String> getUsernames() {
		if (usernames == null) {
			usernames = new ArrayList<String>();
		}
		return usernames;
	}

	public ArrayList<Long> getRelatedTags() {
		if (relatedTags == null) {
			relatedTags = new ArrayList<Long>();
		}
		return relatedTags;
	}

	public void setRelatedTags(ArrayList<Long> relatedTags) {
		this.relatedTags = relatedTags;
	}

	public void setUsernames(ArrayList<String> usernames) {
		this.usernames = usernames;
	}

	public void addUsername(String username) {
		if (!this.usernames.contains(username)) {
			this.usernames.add(username);
		}
	}

	public void delUsername(String username) {
		this.usernames.remove(username);
	}
	
	public void delRelatedUsername(String username) {
		this.relatedUsernames.remove(username);
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ArrayList<Long> getItem_id() {
		return item_id;
	}

	public void setItem_id(ArrayList<Long> item_id) {
		this.item_id = item_id;
	}

	public Long getCreator_id() {
		return creator_id;
	}

	public void setCreator_id(Long creator_id) {
		this.creator_id = creator_id;
	}

	/*
	 * public SignatureMap getTagSignatures() { return tagSignatures; }
	 * 
	 * public void setTagSignatures(SignatureMap tagSignatures) {
	 * this.tagSignatures = tagSignatures; }
	 */

	public Tag() {
		item_id = new ArrayList<Long>();
		user_ids = new ArrayList<Long>();
		tagSignature = "";
		usernames = new ArrayList<String>();
		projectBegin = new Date();
		projectEnd = new Date();
		projectCost = 0.0;
		relatedTags = new ArrayList<Long>();
		relatedUsernames = new ArrayList<String>();
	}

	// FIXME: in constructor initiate everything!
	public Tag(String tagname, Long user_id, String creatorname,
			String tagSignature) {
		this.tagname = tagname;
		item_id = new ArrayList<Long>();
		// tagSignatures = new SignatureMap();
		// tagSignatures.put(user_id, tagSignature);
		creator_id = user_id;
		this.creatorname = creatorname;
		user_ids = new ArrayList<Long>();
		tagSignature = tagSignature;
		user_ids.add(user_id);
		usernames = new ArrayList<String>();
		projectBegin = new Date();
		projectEnd = new Date();
		projectCost = 0.0;
		relatedTags = new ArrayList<Long>();
		relatedUsernames = new ArrayList<String>();
	}

	public Tag(String tagname, Long user_id, String creatorname) {
		this.tagname = tagname;
		item_id = new ArrayList<Long>();
		// tagSignatures = new SignatureMap();
		user_ids = new ArrayList<Long>();
		tagSignature = "";
		creator_id = user_id;
		this.creatorname = creatorname;
		usernames = new ArrayList<String>();
		projectBegin = new Date();
		projectEnd = new Date();
		projectCost = 0.0;
		relatedTags = new ArrayList<Long>();
		relatedUsernames = new ArrayList<String>();
	}

	public String getTagname() {
		return tagname;
	}

	public void setTagname(String tagname) {
		this.tagname = tagname;
	}

	public void addItem(Long itemid) {
		if(!item_id.contains(itemid)) {
			item_id.add(itemid);
		}
	}

	public void delItem(Long itemid) {
		item_id.remove(itemid);
		/*
		 * for(Long id : item_id) { if(id.equals(itemid)) { item_id.remove(id);
		 * } }
		 */
	}

	// **************************************************
	// Übergabe eines Tags
	// ************************************************
	public void addUser(Long user_id, String tagSignature) {
		// FIXME: Bei Übergabe eines Tags tagSignature zu erneuen.
		user_ids.add(user_id);
	}

	public void addUser(Long user_id) {
		// FIXME: Bei Übergabe eines Tags tagSignature zu erneuen.
		user_ids.add(user_id);
	}

	public void delUser(Long user_id) {
		// item_id.remove(itemid);
		int index = user_ids.indexOf(user_id);
		if (index < 0) {
			return;
		} else {
			user_ids.remove(index);
		}
	}

	public void update(Tag t) {
		this.id = t.id;
		this.item_id = t.getItem_id();
		this.tagname = t.getTagname();
		// this.tagSignatures = t.getTagSignatures();
		this.user_ids = t.getUser_ids();
		this.creator_id = t.getCreator_id();
		this.creatorname = t.getCreatorname();
		this.tagSignature = t.getTagSignature();
		this.projectBegin = t.getProjectBegin();
		this.projectEnd = t.getProjectEnd();
		this.projectCost = t.getProjectCost();
		this.usernames = t.getUsernames();
		this.relatedTags = t.getRelatedTags();
		this.relatedUsernames = t.getRelatedUsernames();
	}

	public ArrayList<Long> getUser_ids() {
		return user_ids;
	}

	public void setUser_ids(ArrayList<Long> user_ids) {
		this.user_ids = user_ids;
	}

	public String getTagSignature() {
		return tagSignature;
	}

	public void setTagSignature(String tagSignature) {
		this.tagSignature = tagSignature;
	}

	public Date getProjectBegin() {
		return projectBegin;
	}

	public void setProjectBegin(Date projectBegin) {
		this.projectBegin = projectBegin;
	}

	public Date getProjectEnd() {
		return projectEnd;
	}

	public void setProjectEnd(Date projectEnd) {
		this.projectEnd = projectEnd;
	}

	public Double getProjectCost() {
		return projectCost;
	}

	public void setProjectCost(Double projectCost) {
		this.projectCost = projectCost;
	}

	public boolean equals(Object o) {
		if (this.tagname.equals(((Tag) o).getTagname())
				&& this.getCreator_id() == ((Tag) o).getCreator_id()) {
			return true;
		}
		return false;
	}

	public int hashCode() {
		return 1;
	}
	
	public String toString() {
		return "Tag id: " + this.id 
		+ ", tagname: " + this.tagname
		+ ", begin: " + this.projectBegin
		+ ", end: " + this.projectEnd
		+ ", cost: " + this.getProjectCost()
		+ ", creator: " + this.getCreatorname() 
		+ ", Items: " + this.item_id 
		+ ", shared Benutzer: " + this.getUsernames() 
		+ ", related Tags: " + this.getRelatedTags()
		+ ", related Benutzer: " + this.getRelatedUsernames()
		+ ", tagSignature: " + this.getTagSignature();
	}
}
